if(typeof(Mobile_NS) == 'undefined'){
	Mobile_NS = {};
}
Mobile_NS.addJs = function(jsPath, fn){
	var headEle = document.getElementsByTagName("head")[0]; 
	var scriptEle = document.createElement("script");
	scriptEle.setAttribute("type", "text/javascript"); 
	scriptEle.setAttribute("src", jsPath); 
	if(typeof(fn) == "function"){
		scriptEle.onload = fn;
	}
	headEle.appendChild(scriptEle);
}
var callbackFn = null;
function parseLongLat(longitude, latitude, type){
	function parseAddress(point){
		var geoc = new BMap.Geocoder();
		geoc.getLocation(point, function(rs){
			var addComp = rs.addressComponents;
			var addr = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
			
			var result = {};
			result["status"] = "1";
			result["errMsg"] = "";	//错误信息
			result["lng"] = point.lng;	//精度
			result["lat"] = point.lat;	//纬度
			result["addr"] = addr;	//中文地址
			
			callbackFn.call(this, result);
		});
	}
		
	if(type == "google"){	//谷歌坐标
		var googlePoint = new BMap.Point(longitude, latitude);
		BMap.Convertor.translate(googlePoint, 2, parseAddress);
	}else if(type == "baidu"){	//百度坐标
		var baiduPoint = new BMap.Point(longitude, latitude);
		parseAddress(baiduPoint);
	}else if(type == "gps"){	//原始坐标
		var gpsPoint = new BMap.Point(longitude, latitude);
		BMap.Convertor.translate(gpsPoint, 0, parseAddress);
	}
}

function _p_getLBSResult(result){

	if(result){
		var resultArr = result.split(",");
		if(resultArr.length == 3){
			var gpsstr = result.substring(result.indexOf(",") + 1);	//去掉时间戳，只保留纬度，经度
			parseLongLat(resultArr[2], resultArr[1], "google");
		
		}
	}
}

Mobile_NS.getCurrentPosition = function(fn){
	function innerFn(){
		callbackFn = fn;
		if(navigator.geolocation){
			MLocation.getCurrentPosition(function(position){
				parseLongLat(position.coords.longitude, position.coords.latitude, "gps");
			},function(error){
				var errMsg = "";
				switch(error.code){
				    case error.PERMISSION_DENIED:
				      errMsg = "用户拒绝对获取地理位置的请求。";
				      break;
				    case error.POSITION_UNAVAILABLE:
				      errMsg = "位置信息是不可用的。";
				      break;
				    case error.TIMEOUT:
				      errMsg = "请求用户地理位置超时。";
				      break;
				    case error.UNKNOWN_ERROR:
				      errMsg = "未知错误。";
				      break;
			    }
				var result = {};
				result["status"] = "0";
				result["errMsg"] = errMsg;	//错误信息
				result["lng"] = "";	//精度
				result["lat"] = "";	//纬度
				result["addr"] = "";	//中文地址
				
				fn.call(this, result);
				
			},{
				timeout: 5000,
				enableHighAcuracy: true,
				maximumAge: 3000
			});
		}
	}
		
	if(typeof(BMap) == "undefined"){
		Mobile_NS.addJs("/mobilemode/js/baidu/api_wev8.js", function(){
			Mobile_NS.addJs("/mobilemode/js/baidu/convertor_wev8.js", function(){
				innerFn();
			});
		});
	}else if(typeof(BMap.Convertor) == "undefined"){
		Mobile_NS.addJs("/mobilemode/js/baidu/convertor_wev8.js", function(){
			innerFn();
		});
	}else{
		innerFn();
	}
};